<?php

/**
 * sfGuardUserProfile form base class.
 *
 * @package    form
 * @subpackage sf_guard_user_profile
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $
 */
class BasesfGuardUserProfileForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'user_id'          => new sfWidgetFormInputHidden(),
      'nu_cpf'           => new sfWidgetFormInput(),
      'no_usuario'       => new sfWidgetFormInput(),
      'sobrenome'        => new sfWidgetFormInput(),
      'email'            => new sfWidgetFormInput(),
      'ddd_tel_fixo'     => new sfWidgetFormInput(),
      'tel_fixo'         => new sfWidgetFormInput(),
      'ddd_tel_celular'  => new sfWidgetFormInput(),
      'tel_celular'      => new sfWidgetFormInput(),
      'nu_rg'            => new sfWidgetFormInput(),
      'sexo'             => new sfWidgetFormInput(),
      'dt_nascimento'    => new sfWidgetFormDateTime(),
      'validate'         => new sfWidgetFormInput(),
      'groups_list'      => new sfWidgetFormDoctrineChoiceMany(array('model' => 'sfGuardGroup')),
      'permissions_list' => new sfWidgetFormDoctrineChoiceMany(array('model' => 'sfGuardPermission')),
    ));

    $this->setValidators(array(
      'user_id'          => new sfValidatorDoctrineChoice(array('model' => 'sfGuardUserProfile', 'column' => 'user_id', 'required' => false)),
      'nu_cpf'           => new sfValidatorString(array('max_length' => 14, 'required' => false)),
      'no_usuario'       => new sfValidatorString(array('max_length' => 50)),
      'sobrenome'        => new sfValidatorString(array('max_length' => 150, 'required' => false)),
      'email'            => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'ddd_tel_fixo'     => new sfValidatorInteger(array('required' => false)),
      'tel_fixo'         => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'ddd_tel_celular'  => new sfValidatorInteger(array('required' => false)),
      'tel_celular'      => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'nu_rg'            => new sfValidatorInteger(array('required' => false)),
      'sexo'             => new sfValidatorString(array('max_length' => 1, 'required' => false)),
      'dt_nascimento'    => new sfValidatorDateTime(array('required' => false)),
      'validate'         => new sfValidatorString(array('max_length' => 17, 'required' => false)),
      'groups_list'      => new sfValidatorDoctrineChoiceMany(array('model' => 'sfGuardGroup', 'required' => false)),
      'permissions_list' => new sfValidatorDoctrineChoiceMany(array('model' => 'sfGuardPermission', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('sf_guard_user_profile[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'sfGuardUserProfile';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['groups_list']))
    {
      $this->setDefault('groups_list', $this->object->groups->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['permissions_list']))
    {
      $this->setDefault('permissions_list', $this->object->permissions->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->savegroupsList($con);
    $this->savepermissionsList($con);
  }

  public function savegroupsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['groups_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->groups->getPrimaryKeys();
    $values = $this->getValue('groups_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('groups', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('groups', array_values($link));
    }
  }

  public function savepermissionsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['permissions_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->permissions->getPrimaryKeys();
    $values = $this->getValue('permissions_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('permissions', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('permissions', array_values($link));
    }
  }

}
